home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual Basic 5 / Mastering Microsoft Visual Basic 5.ISO / sampapps / cancelasynch / clsasynch.cls < prev    next >
Text File  |  1997-01-15  |  1KB  |  54 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "clsAsynch"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. Private WithEvents objTimer As CTimer
  12. Attribute objTimer.VB_VarHelpID = -1
  13. Dim iterations As Long
  14. Dim timerID As Long
  15.  
  16. Public Event IAmWorkingOnIt(ByVal percentdone As Long, ByRef Cancel As Boolean)
  17. Public Event TaskComplete()
  18. Public Event TaskCancelled()
  19.  
  20.  
  21. Public Sub SomeAsynchronousTask()
  22.   iterations = 0
  23.   objTimer.StartTimer 200
  24. End Sub
  25.  
  26. Private Sub Class_Initialize()
  27.   Set objTimer = New CTimer
  28. End Sub
  29.  
  30. Private Sub Class_Terminate()
  31.   Set objTimer = Nothing
  32. End Sub
  33.  
  34. Private Sub objTimer_TimerEvent()
  35.   Dim bCancel As Boolean
  36.   iterations = iterations + 1
  37.   If iterations > 100 Then
  38.     RaiseEvent TaskComplete
  39.     objTimer.StopTimer
  40.   Else
  41.     If iterations / 5 = Int(iterations / 5) Then
  42.       ' Send the event every 5 iterations
  43.       RaiseEvent IAmWorkingOnIt(iterations, bCancel)
  44.       ' Because bCancel is declared ByRef, the client can change
  45.       ' its value and the change will be reflected here.
  46.       If bCancel Then
  47.         objTimer.StopTimer
  48.         RaiseEvent TaskCancelled    ' The task has been cancelled
  49.       End If
  50.     End If
  51.   End If
  52. End Sub
  53.  
  54.